import { reqGoodsInfo, reqShoppingCart } from "@/api"
// 封装游客临时身份的模块uuid -----> 生成一个随机字符串
import { getUUID } from '@/utils/uuid_token';

const actions = {
  // 获取产品信息
  async getGoodInfo({ commit }, skuId) {
    let result = await reqGoodsInfo(skuId)
    if (result.code == 200) {
      commit("GETGOODINFO", result.data)
    }
  },
  // 加入购物车
  async addShoppingCart({ commit }, { skuId, skuNum }) {
    // 服务器没有返回其余数据，不需要存储
    let result = await reqShoppingCart(skuId, skuNum)
    if (result.code === 200) {
      return "ok"
    } else {
      return Promise.reject(new Error('加入购物车失败'))
    }
  }
}
const mutations = {
  // 获取产品信息
  GETGOODINFO(state, goodInfo) {
    state.goodInfo = goodInfo
  },
}
const getters = {
  categoryView(state) {
    return state.goodInfo.categoryView || {}
  },
  skuInfo(state) {
    return state.goodInfo.skuInfo || {}
  },
  spuSaleAttrList(state) {
    return state.goodInfo.spuSaleAttrList || []
  }
}
const state = {
  goodInfo: {},
  uuid_token: getUUID()
}

export default {
  actions,
  mutations,
  getters,
  state
}
